//
//  EditController.m
//  ContactViewerIos
//
//  Created by Mitchell Sans Souci on 3/24/12.
//  Copyright (c) 2012 Tiny Mission. All rights reserved.
//

#import "ContactActionViewController.h"
#import "MasterViewController.h"


@implementation ContactActionViewController

NSString* EMPTY_STRING = @"";
NSString* CREATE_TITLE_TEXT = @"Create";
NSString* EDIT_TITLE_TEXT = @"Edit";
NSString* VALIDATION_POPUP_TEXT = @"Name Field is Required.";

@synthesize contact;
@synthesize contacts;
@synthesize action;
@synthesize nameField;
@synthesize titleField;
@synthesize phoneField;
@synthesize emailField;
@synthesize twitterIdField;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/
-(void)viewWillDisappear:(BOOL)animated {
    [self.nameField becomeFirstResponder];
    [self.nameField resignFirstResponder];   
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (CREATE == self.action){
        //set the title
        self.title = CREATE_TITLE_TEXT;
        
        //setting values to null
        self.nameField.text = nil;
        self.titleField.text = nil;
        self.phoneField.text = nil;
        self.emailField.text = nil;
        self.twitterIdField.text = nil;   
    }
    else if (EDIT == self.action && contact) {        
        //set the title
        self.title = EDIT_TITLE_TEXT;
        
        //setting values from Object
        self.nameField.text = self.contact.name;
        self.titleField.text = self.contact.title;
        self.phoneField.text = self.contact.phone;
        self.emailField.text = self.contact.email;
        self.twitterIdField.text = self.contact.twitterId;
    }
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//TODO: validation
-(IBAction)onSaveContact:(id)sender {
    
    if (nil == self.nameField.text || 0 == [self.nameField.text length]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:VALIDATION_POPUP_TEXT  message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil, nil];
        [alert show];   
    }
    else {
        if (nil == self.contact) {
            self.contact = [[Contact alloc] init];
        }
        
        [ContactList initSingleton];
        contacts = [ContactList singleton];
        
        self.contact.name = [self retrieveTextFieldText: self.nameField];
        self.contact.title = [self retrieveTextFieldText: self.titleField];
        self.contact.phone = [self retrieveTextFieldText: self.phoneField];
        self.contact.email = [self retrieveTextFieldText: self.emailField];
        self.contact.twitterId = [self retrieveTextFieldText: self.twitterIdField];
        
        if (CREATE == self.action){
            [contacts addContact:(self.contact)];
        }
        else if (EDIT == self.action){
             [contacts updateContact:(self.contact)];
        }
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
-(NSString*) retrieveTextFieldText:(UITextField*) textField
{
    if (nil != textField.text)
    {
        return textField.text;
    } else {
        return EMPTY_STRING;
    }
}

@end
